package com.kqb.service.impl;

import com.kqb.common.constant.IdPrefixConstant;
import com.kqb.common.domain.AjaxResult;
import com.kqb.common.domain.ErrorResult;
import com.kqb.common.utils.DateUtils;
import com.kqb.common.utils.StringUtils;
import com.kqb.mapper.ErpSalesOrderContractMapper;
import com.kqb.mapper.ErpSalesOrderDetailsMapper;
import com.kqb.mapper.ErpSalesOrderMapper;
import com.kqb.mapper.ErpSalesOrderProductMapper;
import com.kqb.pojo.ErpSalesOrder;
import com.kqb.service.IErpSalesOrderService;
import com.kqb.utils.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * @author 杉果树皮
 * @date 2025/3/6 15:07
 * @description:
 */
@Service
public class ErpSalesOrderServiceImpl implements IErpSalesOrderService {

    @Autowired
    private ErpSalesOrderContractMapper erpSalesOrderContractMapper;

    @Autowired
    private ErpSalesOrderMapper erpSalesOrderMapper;

    @Autowired
    private ErpSalesOrderDetailsMapper erpSalesOrderDetailsMapper;

    @Autowired
    private ErpSalesOrderProductMapper erpSalesOrderProductMapper;
    private String maxSn;

    @Override
    @Transactional
    public AjaxResult save(ErpSalesOrder erpSalesOrder) {
        //获取当前操作对象
        Map currentUser = ThreadLocalUtils.getThreadLocalUtils().getCurrentUser();
        int id = (int) currentUser.get("id");
        String role = ((String[]) currentUser.get("roles"))[0];
        String realName = (String) currentUser.get("userInfo");
        //判断角色
        if ("sale".equals(role)) {
            erpSalesOrder.setSalesId(id);
            erpSalesOrder.setSalesName(realName);
        }
        //保存销售订单
        String sn = generateMaxSn();
        erpSalesOrder.setSn(sn);
        int save = erpSalesOrderMapper.save(erpSalesOrder);
        ErrorResult.business(save, "销售订单", "销售订单保存失败");
        //保存合同
        if (StringUtils.isNotNull(erpSalesOrder.getErpSalesOrderContract())) {
            erpSalesOrder.getErpSalesOrderContract().setRelationSn(sn);
            int save1 = erpSalesOrderContractMapper.save(erpSalesOrder.getErpSalesOrderContract());
            ErrorResult.business(save1, "销售订单", "销售订单合同保存失败");
        }
        //保存技术要求细节
        if (StringUtils.isNotNull(erpSalesOrder.getErpSalesOrderDetails())) {
            erpSalesOrder.getErpSalesOrderDetails().setRelationSn(sn);
            int save2 = erpSalesOrderDetailsMapper.save(erpSalesOrder.getErpSalesOrderDetails());
            ErrorResult.business(save2, "销售订单", "销售订单技术要求细节保存失败");
        }
        //保存商品
        if (StringUtils.isNotNull(erpSalesOrder.getErpSalesOrderProducts()) && erpSalesOrder.getErpSalesOrderProducts().size() > 0) {
            int save3 = erpSalesOrderProductMapper.save(sn, erpSalesOrder.getErpSalesOrderProducts());
            ErrorResult.business(save3, "销售订单", "销售订单产品信息保存失败");
        }
        return AjaxResult.success("添加销售订单成功");
    }

    /**
     * 生成编码
     *
     * @return 编码
     */
    public String generateMaxSn() {
        //获取当前时间 20250306 -> 2503
        String ym = DateUtils.getSysDateYM();
        //内存中不存在
        if (StringUtils.isNull(maxSn)) {
            //查找数据库中是否存在最大编码
            maxSn = erpSalesOrderMapper.getMaxSn();
            if (StringUtils.isNull(maxSn))
                return maxSn = IdPrefixConstant.SALES_ORDER + ym + "0001";
        }

        //内存中存在 SSDJ25030001
        String temp = maxSn.substring(4, 8);
        //不是当月
        if (!ym.equals(temp))
            return maxSn = IdPrefixConstant.SALES_ORDER + ym + "0001";
        int suffix = Integer.parseInt(maxSn.substring(8));
        suffix++;
        return maxSn = IdPrefixConstant.SALES_ORDER + ym + String.format("%04d", suffix);

    }
}
